package com.demo.web.controller;


import com.demo.model.SysUser.SysUser;
import com.demo.service.LoginandRegister.LoginService;
import com.demo.service.LoginandRegister.RegisterService;
import com.demo.service.User.SysUserservice;
import com.demo.tool.JWTUTIL;
import com.demo.tool.ReturnResult;
import com.demo.tool.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;


@RestController
@RequestMapping("/api/student")
public class studentcontroller {


    @Autowired
   private Token tokenTool;
     @Autowired
   private RegisterService registerService;

    @Autowired
    private SysUserservice userservice;

    @Autowired
    private LoginService loginservice;

//    @Autowired
//    private RedisTemplate redisTemplate;

//    @GetMapping("/getuser")
//    public List<User> getUserOne() {
//        List<User> list = new ArrayList<>();
//        User user = userservice.getUserOne(1L);
//        System.out.println(user);
//        list.add(user);
//        return list;
//    }


//    //map返回结果集
//    @PostMapping("/Login")
//    public HashMap<String,Object> Login(@RequestBody User user) {
//        HashMap<String, Object> map = new HashMap<>();
//        String token = null;
//        if (loginservice.verifyUserAndPassword(user.getUsername(),user.getPassword())>0) {
//            //登录成功生成token
////            Token tokenTool;
//            token = tokenTool.createToken();
//        }
//        map.put("message", "登录成功");
//        map.put("token", token);
//////        Serializable username=user.getUsername();
//////        //将token存入redis
//////        tokenTool.depositRedis(token, (String) username);
//////        ValueOperations valueOperations = redisTemplate.opsForValue();
//////        valueOperations.set(token,user.getUid(), 30, TimeUnit.MINUTES);
////        //将token存储到redis中
////        ValueOperations operations = stringRedisTemplate.opsForValue();
////        operations.set(token, user.getUsername());
////        //设置redis中该token的有效时间为20分钟
////        stringRedisTemplate.expire(token, 20, TimeUnit.MINUTES);
//        tokenTool.depositRedis(token,user.getUsername());
//
//        return map;
//    }

    @PostMapping("/Login")
    public ReturnResult Login(@RequestBody SysUser user) {
        String token = null;
        if (loginservice.verifyUserAndPassword(user.getUsername(), user.getPassword()) > 0) {
            //登录成功生成token
//            Token tokenTool;
            token=JWTUTIL.getToken(user.getUid(),user.getUsername());
            System.out.println(user+"用户存在");
//            token = JWTUTIL.getToken(user.getUid(),user.getUsername());
            System.out.println(token+"token");
        }
////        Serializable username=user.getUsername();
////        //将token存入redis
////        tokenTool.depositRedis(token, (String) username);
////        ValueOperations valueOperations = redisTemplate.opsForValue();
////        valueOperations.set(token,user.getUid(), 30, TimeUnit.MINUTES);
//        //将token存储到redis中
//        ValueOperations operations = stringRedisTemplate.opsForValue();
//        operations.set(token, user.getUsername());
//        //设置redis中该token的有效时间为20分钟
//        stringRedisTemplate.expire(token, 20, TimeUnit.MINUTES);

        String tokenresult = tokenTool.tokenIsExpired(user.getUsername());
        //代表当前用户上次的token未过期
        if (tokenresult.length() > 0) {
            token = tokenresult;
        } else {
            tokenTool.depositRedis(user.getUsername(), token);
        }


        return ReturnResult.success(token);
    }


    @GetMapping("/info")
    public String test() {
        return "我是学生";
    }


//    @PostMapping("/register")
//    public ReturnResult register(@RequestBody Register register) throws UnsupportedEncodingException {
//        int result=registerService.Register(register);
//        Boolean flag=result>0?true:false;
//        return ReturnResult.isSuccessOrError(flag);
//    }


    //获取用户登录信息1
    @GetMapping("/user1")
    public Authentication test1(Authentication authentication) {
        return authentication;
    }

    //获取用户登录信息2
    @GetMapping("/user2")
    public Principal test2(Principal principal) {
        return principal;
    }

    //获取用户登录信息3 通过上下文方式获取
    @GetMapping("/user3")
    public Principal test3() {
        return SecurityContextHolder.getContext().getAuthentication();
    }


}
